/*
 * Copyright (c) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.snj.visangedu.listeningmax;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snj.visangedu.R;
import com.snj.visangedu.lib.Global;

/**
 * @author gg@google.com (Your Name Here)
 *
 */
public class Music_Folder {
  private static final String TAG = "StudyPlan";

  private MP_FileManager FM;

  public Activity atvParent;
  private LinearLayout mParent;
  private String pakageName;

  private LinearLayout listParent = null;

  public Music_Folder(Activity atv, LinearLayout p, String szPkg) {
    FM = new MP_FileManager();

    atvParent = atv;
    mParent = p;
    pakageName = szPkg;
  }

  public void Init() {

    if (listParent == null) {
      listParent = (LinearLayout) atvParent.findViewById(R.id.mp_folder_list_layout);

      if (listParent == null) {
        Log.e(TAG, "R.id.mp_folder_list_layout == null");
        return;
      }
      List<MP_FileInfo> adds = FM.FolderListEntrySearch(atvParent, 0);

      
      for (int i = 0; i < adds.size(); i++) {
        AddItem(listParent, adds.get(i));
      }
    }
  }

  public void DirOpenClick(int id) {
    MP_FileInfo info = FM.FolderListFind(id);
    if (info != null) {
      if (info.bIsFile) {
        Log.e(TAG, "DirOpenClick, not Folder id=" + id);
        return;
      }

      LinearLayout parent = (LinearLayout) info.view.findViewById(R.id.folder_sub_win);
      ImageButton btnOpen = (ImageButton) info.view.findViewById(id);// R.id.mp_folder_open);


      if (!info.bOpenFolder) {

        List<MP_FileInfo> adds = FM.FolderMediaSearch(atvParent, id);

        parent.setVisibility(View.VISIBLE);

        for (int i = 0; i < adds.size(); i++) {
          AddItem(parent, adds.get(i));
        }
        btnOpen.setImageResource(R.drawable.music_icon_23);
        info.bOpenFolder = true;
      } else {
        FM.FolderListChild(info.snID);
        parent.removeAllViews();
        parent.setVisibility(View.GONE);
        btnOpen.setImageResource(R.drawable.music_icon_22);
        info.bOpenFolder = false;
      }
    } else
      Log.e(TAG, "DirOpenClick, not found id=" + id);

  }

  public void RenameUpdate(MP_FileInfo info) {
    if (info.bIsFile)
      RenameUpdateFileItem(info);
    else
      RenameUpdateDirItem(info);
  }

  public void RenameUpdateDirItem(MP_FileInfo info) {
    if (info == null) return;
    if (info.bIsFile) return;
    TextView tvMemo = (TextView) info.view.findViewById(R.id.mf_item_memo);
    tvMemo.setText(info.szName);
  }

  public void RenameUpdateFileItem(MP_FileInfo info) {
    if (info == null) return;
    if (!info.bIsFile) return;
    TextView tvMemo = (TextView) info.view.findViewById(R.id.mf_item_song_memo);
    tvMemo.setText(info.szName);
  }

  public void AddItem(LinearLayout parent, MP_FileInfo info) {
    if (info.bIsFile)
      AddItemFile(parent, info);
    else
      AddItemFolder(parent, info);
  }

  public void AddItemFolder(LinearLayout parent, MP_FileInfo info) {
    View v = getItemFolderTypeView();

    // TextView mf_item_memo
    TextView tvMemo = (TextView) v.findViewById(R.id.mf_item_memo);
    tvMemo.setText(info.szName);    
    Global.SetDefaultFont(tvMemo);

    // ImageButton folder_open
    ImageButton btnOpen = (ImageButton) v.findViewById(R.id.mp_folder_open);
    btnOpen.setId(info.snID);
    // ImageButton folder_rename
    ImageButton btnRename = (ImageButton) v.findViewById(R.id.mp_folder_rename);
    btnRename.setId(info.snID + 1);

    info.view = v;
    parent.addView(v);
  }

  public void AddItemFile(LinearLayout parent, MP_FileInfo info) {
    View v = getItemFileTypeView();

    // TextView mf_item_song_memo
    TextView tvMemo = (TextView) v.findViewById(R.id.mf_item_song_memo);
    tvMemo.setText(info.szName);
    Global.SetDefaultFont(tvMemo);

    // ImageButton song_play_add
    ImageButton btnPlayAdd = (ImageButton) v.findViewById(R.id.song_play_add);
    btnPlayAdd.setId(info.snID);

    // ImageButton song_file_rename
    ImageButton btnRename = (ImageButton) v.findViewById(R.id.song_file_rename);
    btnRename.setId(info.snID + 1);

    info.view = v;
    parent.addView(v);
  }

  public View getItemFolderTypeView() {
    try {
      LayoutInflater vi =
          (LayoutInflater) atvParent.getBaseContext().getSystemService(
              Context.LAYOUT_INFLATER_SERVICE);

      View v = vi.inflate(R.layout.y_music_folder_list_item, null);
      return v;
    } catch (Exception e) {
      Log.e(TAG, "getItemFolderTypeView, #ERROR " + e.getMessage());
      return null;
    }
  }

  public View getItemFileTypeView() {
    try {
      LayoutInflater vi =
          (LayoutInflater) atvParent.getBaseContext().getSystemService(
              Context.LAYOUT_INFLATER_SERVICE);

      View v = vi.inflate(R.layout.y_music_folder_list_item_song, null);
      return v;
    } catch (Exception e) {
      Log.e(TAG, "getItemFolderTypeView, #ERROR " + e.getMessage());
      return null;
    }
  }

}
